<?php

class Dao_AbstractDAO {
	protected $db;
	protected $tableName;
	protected $key;
	protected $classModel;

	protected function __construct($db = null) {
		if (! isset($db) || $db === null) {
			$db = new mysqli(DB_HOST, DB_USERNAME, DB_PASS, DB_DATABASE_NAME);
			$this->db = $db;
			$db->query('set names "utf8"');
		} else {
			$this->db = $db;
		}
	}

	public function getAll() {
		$query = "select * from " . $this->tableName;
		return $this->getObjects($query);
	}

	protected function getObjects($res) {
		$db = $this->db;
		
		$value = array();
		
		if ($res) {
			while ( $row = $res->fetch_assoc() ) {
				$value[] = new $this->classModel($row);
			}
		}
		
		return $value;
	}

	public function startTransaction() {
		$db = $this->db;
		$db->autocommit(FALSE);
	}

	public function commitTransaction() {
		$db = $this->db;
		$db->commit();
		$db->autocommit(TRUE);
	}

	public function rollBackTransaction() {
		$db = $this->db;
		$db->rollback();
		$db->autocommit(TRUE);
	}

}